<template>
<div>
<!--导航栏-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品分类</el-breadcrumb-item>
  </el-breadcrumb>

<!--卡片视图-->
  <el-card>
    <!--添加分类按钮-->
    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>

    <!--分类列表-->
      <tree-table
        ref="table"
        :data="cateList"
        :columns="columns"
        :stripe="false"
        :border="true"
        :show-row-hover="false"
        :show-index="true"
        :expand-type="false"
        :selection-type="false"
      >
        <template slot="isValid" scope="scope">
          <i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="sort" scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level===1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level===2" type="warning">三级</el-tag>
        </template>
        <template slot="option" scope="scope">
          <el-button size="mini" class="el-icon-edit" type="primary" @click="showEditCateDialog(scope.row.cat_id)">编辑</el-button>
          <el-button size="mini" class="el-icon-delete" type="danger" @click="deleteCate(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>

    <!--分页器-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>

  <!--添加分类对话框-->
  <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form label-width="80px" :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectKeys"
            :options="parentCateList"
            :props="cascaderProps"
            clearable
            @change="parentCateChange"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
      </template>
    </el-dialog>

  <!--编辑分类对话框-->
  <el-dialog
    title="编辑分类"
    :visible.sync="editCateDialogVisible"
    width="50%"
    @close="editCateDialogClosed"
  >
    <el-form label-width="80px" :model="editCateForm" :rules="editCateFormRules" ref="editCateFormRef">
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="editCateForm.cat_name"></el-input>
      </el-form-item>
      <el-form-item label="父级分类">
        <el-cascader
          v-model="selectKeys"
          :options="parentCateList"
          :props="cascaderProps"
          clearable
          @change="parentCateChange"
          disabled
        >
        </el-cascader>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="editCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      cateList: [],
      // 查询栏查询信息
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 数据条数
      total: 0,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isValid' },
        { label: '排序', type: 'template', template: 'sort' },
        { label: '操作', type: 'template', template: 'option' }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      },
      // 获取的父级分类列表
      parentCateList: [],
      selectKeys: [],
      cascaderProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      editCateDialogVisible: false,
      editCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      editCateFormRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品分类列表失败')
      this.$message.success('获取商品分类列表成功')
      this.cateList = res.data.result
      this.total = res.data.total
    },

    // 更新页码导航
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (current) {
      this.queryInfo.pagenum = current
      this.getCateList()
    },

    // 显示添加分类对话框
    showAddCateDialog () {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },

    // 添加分类对话框对话框关闭重置数据
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },

    // 提交添加分类请求
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },

    // 获取父级分类列表
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
      this.$message.success('获取父级分类列表成功')
      this.parentCateList = res.data
    },

    // 获取全级分类列表
    async getAllCateList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
      this.$message.success('获取父级分类列表成功')
      this.parentCateList = res.data
    },

    // 父级分类点击后改变列表数据
    parentCateChange () {
      if (this.selectKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addCateForm.cat_level = this.selectKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },

    // 显示编辑分类对话框并获取分类数据
    async showEditCateDialog (id) {
      this.editCateDialogVisible = true
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('获取分类数据失败')
      this.$message.success('获取分类数据成功')
      this.editCateForm = res.data
    },

    // 编辑分类对话框关闭重置数据
    editCateDialogClosed () {
      this.$refs.editCateFormRef.resetFields()
      this.selectKeys = []
      this.editCateForm.cat_pid = 0
      this.editCateForm.cat_level = 0
    },

    // 删除分类
    async deleteCate (id) {
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$message.info('取消删除')
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      this.getCateList()
    }
  }
}
</script>

<style scoped>
.zk-table{
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>
